﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Media;

namespace AircraftBattleClient
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Window
    {
        bool isadmin;
        static GameEngineServiceClient clt = new GameEngineServiceClient();
        string username;
        string password;
        SoundPlayer sndHover = new SoundPlayer(Constants.SoundFolderRelativePath
                + "\\" + "HoverMouse.wav");
        SoundPlayer sndClick = new SoundPlayer(Constants.SoundFolderRelativePath
                + "\\" + "ClickButton.wav");

        public Login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            sndClick.Play();
            username = this.txtUserName.Text.Trim();
            password = this.txtPassword.Password.Trim();
            if (string.IsNullOrEmpty(username)) return;

            if (!clt.IfUserExist(username))
            {
                string temp = clt.CreateUser(username);
                if (temp != "success")
                {
                    Message messagewindow = new Message(temp);
                    messagewindow.Show();
                    return;
                }      
            }
            else if (string.IsNullOrEmpty(password))
                return;
            else if (!password.Equals(clt.GetPassword(username)))
                return;

            if (clt.GetBlockInfo(username))
            {
                this.lblBlock.Visibility = System.Windows.Visibility.Visible;
                return;
            }

            if (clt.IsAdmin(username))
            {
                AdminLounge adminLounge = new AdminLounge(username);
                adminLounge.Show();
            }
            else
            {
                GameLounge gameLounge = new GameLounge(username);
                gameLounge.Show();
            }

            this.Close();
        }

        private void button1_MouseEnter(object sender, MouseEventArgs e)
        {
            sndHover.Play();
            //button1.Background = button1.Foreground;
        }
    }
}
